﻿Imports utilitiesLibrary
Imports ecomLibrary

Public Class storefront
    Inherits System.Web.UI.Page

    Dim cookieArray As HttpCookieCollection
    Dim myCookie As New HttpCookie("cis342")
    Dim objDB As DBConnect
    Dim userID As String
    Dim cookieList() As String
    Dim ds As New DataSet
    Dim srlReadWrite As New Serialize
    Dim shopCart As New cart

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        objDB = Session("db")

        'find cookies
        If Not IsPostBack Then
            cookieList = Request.Cookies.AllKeys
            'MsgBox(cookieList.Length)
            If cookieList.Length = 0 Or cookieList.Length = 1 Then
                txtLoginID.Text = ""
                Session("login") = Nothing
            Else
                Try
                    lblLoginInfo.Text = "Enter your password, or if this is not your email address, enter the correct email and password below.  "
                    userID = Request.Cookies("cis342").Value
                    txtLoginID.Text = userID
                Catch x As Exception
                    'MsgBox("NOPE")
                End Try
            End If
        End If

    End Sub

    Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLogin.Click
        'Dim userID As String
        Dim pass As String
        Dim dbPass As String
        Dim sql As String

        If txtLoginID.Text = "" Then
            lblLoginInfo.Text = "USERNAME MUTHAF***A! DO YOU ENTER IT?"
            Exit Sub
        End If

        userID = txtLoginID.Text
        pass = txtLoginPass.Text

        Try
            sql = "SELECT Password FROM mall_customer WHERE LoginID='" & userID & "'"
            objDB.getDataSet(sql)
            dbPass = objDB.getField("Password", 0)

        Catch ex As Exception
            lblLoginInfo.Text = "There was a problem connecting to the databse. Sucks for you."
            Exit Sub
        End Try


        If pass <> dbPass Then
            lblLoginInfo.Text = "Well now, I don't think that was the correct password at all. Try again, n00b."
            txtLoginPass.Text = ""
            Exit Sub
        End If

        If txtLoginID.Text = "" Or txtLoginPass.Text = "" Then
            lblLoginInfo.Text = "WTF BRO!? See those 2 boxes? FILL 'EM BOTH IN DAWG!"
        Else
            'send cookie to browser
            If chkDelCookie.Checked = True And chkRememLogin.Checked = True Then
                lblLoginInfo.Text = "Please select only 1 checkbox."
                Exit Sub
            End If

            If chkRememLogin.Checked = True Then
                myCookie.Value = userID
                myCookie.Expires = DateAndTime.Now.AddDays(14)
                Response.Cookies.Add(myCookie)
            End If

            If chkDelCookie.Checked = True Then
                myCookie.Expires = DateTime.Now.AddDays(-1D)
                Response.Cookies.Add(myCookie)
            End If

        End If

        Session("login") = userID

        clearLoginForms()

        'deserialize cart and store in session variable
        shopCart = srlReadWrite.ReadCartS(userID)
        If shopCart Is Nothing Then
            shopCart = New cart
            shopCart.LoginID = userID
        End If

        Session("cart") = shopCart

        Response.Redirect("../Shopping/store.aspx")
    End Sub

    Protected Sub btnRegister_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnRegister.Click
        Dim pass As String
        Dim userName As String
        Dim shipAddy As String
        Dim sql As String
        Dim isValidInsert As Integer
        'Dim isValidSerialize As Integer
        Dim newCart As New cart

        lblRemInfo.Text = ""

        userID = txtRegisterID.Text
        userName = txtRegisterName.Text
        shipAddy = txtRegisterAddress.Text
        pass = txtRegisterPass.Text

        If System.Text.RegularExpressions.Regex.IsMatch(userID, "^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,3})$") Then
        Else
            lblRemInfo.Text = "Please enter a valid email address."
            Exit Sub
        End If

        If userID = "" Or userName = "" Or shipAddy = "" Or pass = "" Then
            lblRemInfo.Text = "Please ensure you have fulled out the registration form completely."
            Exit Sub
        End If

        If chkRememReg.Checked = True Then
            myCookie.Value = userID
            myCookie.Expires = DateAndTime.Now.AddDays(14)
            Response.Cookies.Add(myCookie)
        End If

        sql = "INSERT INTO mall_customer (LoginID, CustName, ShipAddress, Password, TotalDollarSales) VALUES('" & userID & "', '" & userName & "', '" & shipAddy & "', '" & pass & "', 0)"
        isValidInsert = objDB.doUpdate(sql)

        'cannot find store procedure WriteCart
        'srlReadWrite.WriteCartS(newCart, userID)
        'If isValidSerialize = -1 Then
        '    MsgBox("error serializing.")
        '    Exit Sub
        'End If

        If isValidInsert = -1 Then
            lblRemInfo.Text = "Account already exists with that email address. Remember your password, fool."
            clearLoginForms()
            Exit Sub
        Else
            lblRemInfo.Text = "Account for " & userID & " has been succesfully created. You may proceed to log in."
            'Response.Redirect("storefront.aspx")
        End If


    End Sub

    Protected Sub clearLoginForms()
        txtLoginID.Text = String.Empty
        txtLoginPass.Text = String.Empty
        txtRegisterAddress.Text = String.Empty
        txtRegisterID.Text = String.Empty
        txtRegisterName.Text = String.Empty
        txtRegisterPass.Text = String.Empty
        chkRememLogin.Checked = False
        chkRememReg.Checked = False
    End Sub
End Class